package com.platform.comm.common.batch.listener;


import com.platform.comm.common.batch.ComBatchContext;
import lombok.extern.log4j.Log4j2;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;

/**
 * Description: job任务监听器
 * @author lx
 */
@Log4j2
public class ComImportJobListener extends JobExecutionListenerSupport {

    private ComBatchContext comBatchContext;
    public ComImportJobListener(ComBatchContext comBatchContext ) {
        this.comBatchContext = comBatchContext;
    }

    @Override
    public void beforeJob(JobExecution jobExecution) {
        log.info("通用批量导入任务开始启动");
        try {
            // 线程暂停2秒 同步
            Thread.sleep(500);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            try {
                // 修改记录信息
                comBatchContext.finishImportJob();
                log.debug("通用批量导入任务(数据校验关联环节)已经完成");
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }else{
            // 修改记录信息
            comBatchContext.finishImportErrorJob();
        }
        jobExecution.getExecutionContext().remove("config");
    }

}
